package com.ydcycloud.sys.listener;


import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;

import com.smart.mvc.listener.InitListener;
import com.ydcycloud.sys.model.SessionLoginTime;

public class DcsecContextLoaderListener extends InitListener {
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		
		super.contextInitialized(event);
		
		//初始化
		ServletContext servletContext = (ServletContext) event.getServletContext();
		servletContext.setAttribute("path", servletContext.getContextPath());
		
		//初始化枚举接口
		/*ClasspathPackageScanner classScanner=new ClasspathPackageScanner("com.dcsec");
		List<String> enumClassNameList=classScanner.getFullyQualifiedClassNameList(EnumItemable.class);
		servletContext.setAttribute(EnumItemable.class.getSimpleName(), enumClassNameList);*/
	}
	
	@Override
	public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
		
		HttpSession httpSession=httpSessionEvent.getSession();
		SessionLoginTime sessionLoginTime=(com.ydcycloud.sys.model.SessionLoginTime) httpSession.getAttribute(SessionLoginTime.SESSION_LOGIN_TIME);
		if(sessionLoginTime!=null && sessionLoginTime.getDurationTime()>0){
			
			//统计用户登录时间
			
		/*	UserService userService=SpringUtils.getBean("userService");
			SessionUser sessionUser=(SessionUser) httpSession.getAttribute("session_user");
			if(sessionUser!=null){
				User loginUser = userService.get(sessionUser.getId());
				loginUser.setLoginTime(loginUser.getLoginTime()+sessionLoginTime.getDurationTime());
				userService.save(loginUser);
			}*/
		}
		
		super.sessionDestroyed(httpSessionEvent);
	}
}
